#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2023/12/15 12:18
# @Author  : FuKai ZHANG
# @File    : log_decorator.py
# @Description :日志打印装饰器

"""
日志装饰器，控制程序日志输入，默认为 True
如设置 False，则程序不会打印日志
"""

from functools import wraps

from utils.logging_tools.log_control import INFO, ERROR


def log_decorator(switch: bool):
    """
    封装日志装饰器, 打印请求信息
    :param switch: 定义日志开关
    :return:
    """

    def decorator(func):
        @wraps(func)
        def swapper(*args, **kwargs):

            # 判断日志为开启状态，才打印日志
            case = func(*args, **kwargs)
            # 判断日志开关为开启状态
            if switch:
                _log_msg = f"\n======================================================\n" \
                           f"用例标题: {case.detail}\n" \
                           f"请求路径: {case.url}\n" \
                           f"请求方式: {case.method}\n" \
                           f"请求头:   {case.request_headers}\n" \
                           f"请求内容: {case.request_data}\n" \
                           f"预期结果: {case.expected}\n" \
                           f"接口响应内容: {case.response_data}\n" \
                           f"Http状态码: {case.status_code}\n" \
                           f"接口耗时: {case.response_time}\n" \
                           "====================================================="
                if case.status_code == 200:
                    INFO.info(_log_msg)
            return case

        return swapper

    return decorator
